package com.app.asktom.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

//JsonHelper.getInstance().responseRegistration("{\"stat\":\"OK\",\"msg\":\"A confirmation is send to your phone. Please check SMS.\"}");

public class JsonHelper {
	protected StringBuilder sb = new StringBuilder();
	
	public static JsonHelper getInstance() {
		return JsonHolder.INSTANCE;
	}
	
    private static class JsonHolder { 
        public static final JsonHelper INSTANCE = new JsonHelper();
    }
	
    public Map<String,String> responseActivation(String response) {
    	Map<String,String> map = new HashMap<String,String>();
		if(response != null) {
			try {
				JSONObject jsonObject = new JSONObject(response);
				String stat = jsonObject.getString("stat");
				String msg = jsonObject.getString("msg");
				map.put("stat", stat);
				map.put("msg", msg);
			} catch (JSONException e) {
				e.printStackTrace();
			}	
		}    	
    	return map;
    }
    
    public Map<String,String> reponseSendFriends(String response) {
    	Map<String,String> map = new HashMap<String, String>();
    	if(response != null) {
    		try {
    			JSONObject jsonObject = new JSONObject(response);
    			String stat = jsonObject.getString("stat");
    			String msg = jsonObject.getString("msg");
    			map.put("stat", stat);
    			map.put("msg", msg);
    		} catch(JSONException e) {
    			e.printStackTrace();
    		}
    	}
    	return map;
    }
    
	public Map<String,String> responseRegistration(String response) {
		Map<String,String> map = new HashMap<String,String>();
		
		if(response != null) {
			try {
				JSONObject jsonObject = new JSONObject(response);
				
				String stat = jsonObject.getString("stat");
				String msg = jsonObject.getString("msg");
				
				if(stat.equals("OK")) {
					map.put("akey", jsonObject.getString("akey"));
				} else {
					
				}
				map.put("stat", stat);
				map.put("msg", msg);
				
			} catch (JSONException e) {
				e.printStackTrace();
			}	
		}
		return map;
	}
	
	public void addFriend(String phoneNumber) {
		if(sb.length() > 0) {
			sb.append(",");
		} else {
			sb.append("[");
		}
		sb.append("{\"FRIEND\":\"");
		sb.append(phoneNumber);
		sb.append("\"}");
	}
	
	public String getAllFriend() {
		sb.append("]");
		return sb.toString();
	}
	
	public ArrayList<Map<String,String>> responseViewFood(
			String response) {
		ArrayList<Map<String,String>> list = 
				new ArrayList<Map<String,String>>();
		if(response != null) {
			if(response.startsWith("{")) {
				try {
					JSONObject jsonObject = new JSONObject(response);
					Map<String, String> map = new HashMap<String,
							String>();
					map.put("msg", "No photo found. Please upload" +
							" photo");
					map.put("stat", jsonObject.getString("stat"));
					list.add(map);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					
					JSONArray jsonArray = new JSONArray(response);
					for(int i = 0; i < jsonArray.length(); i++) {
						JSONObject jsonObject = 
								jsonArray.getJSONObject(i);
						Map<String,String> map = 
								new HashMap<String, String>();
						map.put("S3id", jsonObject.getString("S3id"));
						map.put("S3coordFull", 
								jsonObject.getString("S3coordFull"));
						map.put("S3jsonadd", 
								jsonObject.getString("S3jsonadd"));
						map.put("S3categ", 
								jsonObject.getString("S3categ"));
						map.put("S3establishment", 
								jsonObject.getString("S3establishment"));
						map.put("S3name", jsonObject.getString("S3name"));
						map.put("S3tags", jsonObject.getString("S3tags"));
						map.put("S3picloc",
								jsonObject.getString("S3picloc"));
						map.put("S3meter",
								jsonObject.getString("S3meter"));
						map.put("UFfriendunedited",
								jsonObject.getString("UFfriendunedited"));
						
						JSONArray jsonArray2 = new JSONArray(jsonObject.getString("S3total"));
						JSONObject objSpoon = jsonArray2.getJSONObject(0);
						
						map.put("S3total", objSpoon.getString("SPOONS"));
						
						String coor[] =  jsonObject.getString("S3coordFull").split(",");
						
						map.put("xCoordinate", coor[0]);
						map.put("yCoordinate", coor[1]);
						
//						JSONObject jObject = new JSONObject(jsonObject.getString(""))
						
						
						list.add(map);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}					
			}
			
			

		}		
		return list;
	}
	
	public Map<String,String> reponseGetStat(String response) {
    	Map<String,String> map = new HashMap<String, String>();
    	if(response != null) {
    		try {
    			JSONObject jsonObject = new JSONObject(response);
    			String stat = jsonObject.getString("stat");
    			String msg = jsonObject.getString("msg");
    			map.put("stat", stat);
    			map.put("msg", msg);
    		} catch(JSONException e) {
    			e.printStackTrace();
    		}
    	}
    	return map;
    }
	
	public ArrayList<Map<String,String>> responseSearchFood(String response) {
		ArrayList<Map<String,String>> list = 
				new ArrayList<Map<String,String>>();
		if(response != null) {
			if(response.startsWith("{")) {
				try {
					JSONObject jsonObject = new JSONObject(response);
					Map<String, String> map = new HashMap<String,
							String>();
					map.put("msg", "Search not found");
					map.put("stat", jsonObject.getString("stat"));
					list.add(map);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					
					JSONArray jsonArray = new JSONArray(response);
					for(int i = 0; i < jsonArray.length(); i++) {
						JSONObject jsonObject = 
								jsonArray.getJSONObject(i);
						Map<String,String> map = 
								new HashMap<String, String>();
						map.put("S3id", jsonObject.getString("S3id"));
						map.put("S3coordFull", 
								jsonObject.getString("S3coordFull"));
						map.put("S3jsonadd", 
								jsonObject.getString("S3jsonadd"));
						map.put("S3categ", 
								jsonObject.getString("S3categ"));
						map.put("S3establishment", 
								jsonObject.getString("S3establishment"));
						map.put("S3name", jsonObject.getString("S3name"));
						map.put("S3tags", jsonObject.getString("S3tags"));
						map.put("S3picloc",
								jsonObject.getString("S3picloc"));
						map.put("S3meter",
								jsonObject.getString("S3meter"));
						map.put("UFfriendunedited",
								jsonObject.getString("UFfriendunedited"));
						
						String coor[] =  jsonObject.getString("S3coordFull").split(",");
						
						map.put("xCoordinate", coor[0]);
						map.put("yCoordinate", coor[1]);
//						JSONObject jObject = new JSONObject(jsonObject.getString(""))
						
						
						list.add(map);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}					
			}
		}
		return list;
	}
}
